// 版权归陈超所有


#include "Characters/CC_CharacterBase.h"

#include "AbilitySystem/CC_AbilitySystemComponent.h"

// #include "AttributeSet.h"

// 设置默认值
ACC_CharacterBase::ACC_CharacterBase()
{
	// 将此字符设置为每帧调用Tick（）。如果你不需要它，你可以关闭它来提高性能。
	PrimaryActorTick.bCanEverTick = false;

	//将武器添加到角色身上，并且武器跟随某个骨骼节点移动，形成手握武器效果。
	Weapon = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Weapon"));	//创建默认子对象
	Weapon->SetupAttachment(GetMesh(), FName("WeaponHandSocket"));		//添加到父节点
	Weapon->SetCollisionEnabled(ECollisionEnabled::NoCollision);		//关闭骨骼网格体碰撞
	
}

//重写接口函数，获取能力系统组件
UAbilitySystemComponent* ACC_CharacterBase::GetAbilitySystemComponent() const
{
	return AbilitySystemComponent;
}


// 在游戏开始或生成时调用
void ACC_CharacterBase::BeginPlay()
{
	Super::BeginPlay();
	
}

void ACC_CharacterBase::InitCharacterASC()
{
}

void ACC_CharacterBase::ApplyEffectToSelf(TSubclassOf<UGameplayEffect> GameplayEffectClass, float Level) const
{
	check(GameplayEffectClass);
	check(IsValid(GetAbilitySystemComponent()));
	FGameplayEffectContextHandle ContextHandle = GetAbilitySystemComponent()->MakeEffectContext();
	ContextHandle.AddSourceObject(this);		//设置源对象，可以通过Spec.GetContext().GetSourceObject()去获取源对象
	const FGameplayEffectSpecHandle SpecHandle = GetAbilitySystemComponent()->MakeOutgoingSpec(GameplayEffectClass, Level, ContextHandle);
	GetAbilitySystemComponent()->ApplyGameplayEffectSpecToSelf(*SpecHandle.Data.Get());
}

void ACC_CharacterBase::InitCharacterDefaultAttribute() const
{
	ApplyEffectToSelf(DefualtPrimaryAttributes, 1.f);
	ApplyEffectToSelf(DefualtSecondaryAttributes, 1.f);
	ApplyEffectToSelf(DefualtVitalAttributes, 1.f);
}

void ACC_CharacterBase::AddCharacterAbilities()
{
	if (CC_AbilitySystemComponent == nullptr)
	{
		CC_AbilitySystemComponent = CastChecked<UCC_AbilitySystemComponent>(AbilitySystemComponent);
	}
	CC_AbilitySystemComponent->AddCharacterAbilities(StartupAbilities);
}



